﻿/*----------------------------------------------------------------
  //Copyright (C) 2012 赵国伟
  //文件功能描述：基础数据管理Ajax请求服务端处理程序。
  //创建人：赵国伟
  //生成时间：2012/04/21 14:15:00
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Shopping.Common;
using Shopping.Model;
using Shopping.BLL;

public partial class AS_AjaxCommon : System.Web.UI.Page
{
    private readonly ProvinceCityAreasManage bllArea = new ProvinceCityAreasManage();
    private readonly UsersManage bllUser = new UsersManage();
    private ReturnValue ret = new ReturnValue();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string action = Request.QueryString["Action"] as string;
            switch (action)
            {

                case "GetCitiesByProvinceID":   //查询指定省份下的城市信息；

                    GetCitiesByProvinceID();
                    Response.End();
                    break;
                case "GetAreasByCityID": //查询指定城市下的区域信息；by 赵国伟 2012-04-04
                    GetAreasByCityID();
                    Response.End();
                    break;
                case "showshoppingcartCount":
                    ShowShoppingCartCount();
                    Response.End();
                    break;
                case "ShowUserStatus":
                    ShowUserStatus();
                    Response.End();
                    break;
                case "SendEval":
                    SendEval();
                    Response.End();
                    break;
                case "SendConsult":
                    SendConsult();
                    Response.End();
                    break;
                case "GetAddressByAreaID":
                    GetAddressByAreaID();
                    Response.End();
                    break;
                case "shoppingcartdetail":
                    ShoppingCartDetail();
                    break;
                case "GetUserBaseInfo":  // 获取当前用户的基本信息；by 赵国伟 2012-08-10
                    GetUserBaseInfo();
                    break;

            }

        }

    }

    #region "获取当前用户的基本信息  by 赵国伟 2012-08-10"
    /// <summary>
    /// 获取当前用户的基本信息
    /// </summary>
    private void GetUserBaseInfo()
    {
        UserBaseInfo u = bllUser.GetUserBaseInfo();
       
        Response.Write(JSONHelper.ObjectToJson<UserBaseInfo>(u));
    }
    #endregion


    private void ShoppingCartDetail()
    {
        ShoppingCartsManage smanage = new ShoppingCartsManage();
        var listinfo = smanage.QueryList(smanage.GetShoppingCartCookieName(0));
        var total = 0;

        var totalprice = 0.0m;
        var html = "<dl>";
        if (listinfo != null && listinfo.Count > 0)
        {
            for (var i = 0; i < listinfo.Count; i++)
            {
                total += listinfo[i].CartInfo.Quantity;
                totalprice += listinfo[i].CartInfo.Quantity * listinfo[i].CartInfo.Price;

                html += "<dd><a href='/goods/" + listinfo[i].CartInfo.GoodsID + ".html'>" + listinfo[i].GoodsName + "</a></dd><dt><font color='red'>" + (listinfo[i].CartInfo.Price.ToString("C2") + "</font>x" + listinfo[i].CartInfo.Quantity) + "</dt>";
            }
            html += "<dd style='width:10%;'>&nbsp;</dd><dt  style='width:80%;'>共" + total + "件商品 总金额<font color='red'>" + (totalprice.ToString("C2")) + "</font></dt><dd style='width:10%;'>&nbsp;</dd><dt  style='width:80%;'><a href='/home/shoppingcarts.aspx'>去结算</a></dt>";
        }
        else
        {
            html += "<dd style='width:0%;'>&nbsp;</dd><dt  style='width:100%;text-align:center;'>购物车中还没有商品，赶紧选购吧！</dt>";
        }
        html += "</dl>";
        Response.Write(html);
    }

    private void SendConsult()
    {
        UsersManage umanage = new UsersManage();
        var users = umanage.GetLoginStatus();
        if (users == null || users.UserID <= 0)
        {
            Response.Write(JSONHelper.ObjectToJson(new ReturnValue { Result = ResultType.Fail, Message = "系统提示：您还没有登录，登录后才能发布咨询！" }));
            return;
        }
        var content = HttpUtility.UrlDecode(Utility.P<string>("content"));
        var goodsId = Utility.P<int>("goodsid");
        ConsultManage consultmanage = new ConsultManage();
        GoodsConsults cons = new GoodsConsults();
        cons.ConsultContent = content;
        cons.ConsultDate = DateTime.Now;
        cons.GoodsID = goodsId;
        cons.UserID = users.UserID;
        cons.ReplyContent = "";
        ReturnValue ret = consultmanage.Add(cons);
        if (ret.IsSucc)
        {
            var g = new GoodsConsultInfo();
            g.UserName = users.UserName;
            g.Consult = cons;
            ret.Message = JSONHelper.ObjectToJson(g);

            Response.Write(JSONHelper.ObjectToJson(ret));
        }
        else
        {
            Response.Write(JSONHelper.ObjectToJson(ret));
        }
    }

    private void GetAddressByAreaID()
    {
        var areaid = Utility.G<int>("areaid");
        if (areaid <= 0)
        {
            Response.Write("");
        }
        ProvinceCityAreasManage bllArea = new ProvinceCityAreasManage();
        var area = bllArea.GetArea(areaid);
        Response.Write("{province:{ID:" + area.Province.ID + ",Name:'" + area.Province.Name + "'},city:{ID:" + area.Area.CityID + ",Name:'" + area.CityName + "'},area:{ID:" + areaid + ",Name:'" + area.Area.AreaName + "'}}");
    }

    private void SendEval()
    {
        UsersManage umanage = new UsersManage();
        var users = umanage.GetLoginStatus();
        if (users == null || users.UserID <= 0)
        {
            Response.Write(JSONHelper.ObjectToJson(new ReturnValue { Result = ResultType.Fail, Message = "不允许匿名评价商品，请登录后操作" }));
            return;
        }
        var startCount = Utility.P<int>("startcount");
        var content = HttpUtility.UrlDecode(Utility.P<string>("content"));
        var goodsId = Utility.P<int>("goodsid");
        EvaluateManage evalmanage = new EvaluateManage();
        GoodsEvaluates eval = new GoodsEvaluates();
        eval.EvaluateContent = content;
        eval.EvaluateDate = DateTime.Now;
        eval.GoodsID = goodsId;
        eval.EvaluateScores = startCount;
        eval.UserID = users.UserID;
        ReturnValue ret = evalmanage.Add(eval);
        if (ret.IsSucc)
        {
            var g = new GoodsEvaluateInfo();
            g.Evaluate = eval;
            g.UserName = users.UserName;

            ret.Message = JSONHelper.ObjectToJson(g);

            Response.Write(JSONHelper.ObjectToJson(ret));
        }
        else
        {
            Response.Write(JSONHelper.ObjectToJson(ret));
        }
    }
    #region "查询指定省份下的城市信息； by 赵国伟 2012-04-04"
    /// <summary>
    /// 查询指定省份下的城市信息；
    /// </summary>
    private void GetCitiesByProvinceID()
    {
        int provinceId = Utility.G<int>("ProvinceID");
        List<IDNameObj> listOfData = bllArea.QueryCities(provinceId);
        if (listOfData != null)
            Response.Write(JSONHelper.ObjectToJson<List<IDNameObj>>(listOfData));
    }
    #endregion

    #region "查询指定城市下的区域信息 by 赵国伟 2012-04-04"
    /// <summary>
    /// 查询指定城市下的区域信息
    /// </summary>
    private void GetAreasByCityID()
    {
        int cityId = Utility.G<int>("CityID");
        List<IDNameObj> listOfData = bllArea.QueryArea(cityId);
        if (listOfData != null)
            Response.Write(JSONHelper.ObjectToJson<List<IDNameObj>>(listOfData));
    }
    #endregion

    private void ShowShoppingCartCount()
    {
        UsersManage bllUser = new UsersManage();
        Users u = bllUser.GetLoginStatus();
        var userID = u.UserID;

        ShoppingCartsManage shoppingCart = new ShoppingCartsManage();
        var list = shoppingCart.QueryList(shoppingCart.GetShoppingCartCookieName(userID));
        Response.Write(list == null ? 0 : list.Count);
    }

    private void ShowUserStatus()
    {
        UsersManage bllUser = new UsersManage();
        Users u = bllUser.GetLoginStatus();
        IDNameObj n = new IDNameObj();
        if (u == null || u.UserID == 0)
        {
            n.ID = 0;
        }
        else
        {
            n.ID = u.UserID;
            n.Name = u.UserName;
        }

        Response.Write(JSONHelper.ObjectToJson(n));

        Response.End();
    }

}